package com.zh.oauth.service.impl;

import com.alibaba.fastjson.JSON;
import com.zh.common.constans.AuthConstant;
import com.zh.common.entity.ElecMenu;
import com.zh.oauth.dao.ElecUserDaoMapper;
import com.zh.oauth.entity.ElecAuth;
import com.zh.oauth.entity.ElecRole;
import com.zh.oauth.entity.ElecUser;
import com.zh.oauth.securety.UserDetailsServiceImpl;
import com.zh.oauth.service.ElecUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description:
 * @Author: Li.HuiNeng
 * @Create in: 2019-09-24 10:21
 * @Version: 1.0.1
 */
@Service
public class ElecUserServiceImpl implements ElecUserService {

    @Autowired
    private UserDetailsServiceImpl service;

    @Autowired
    private ElecUserDaoMapper dao;

    @Override
    public ElecUser selectUserDetail(String userLoginName) {
        return dao.selectUserDetail(userLoginName);
    }

    @Override
    public List<ElecRole> selectRoleByUserId(Long userId) { return dao.selectRoleByUserId(userId); }

    @Override
    public List<ElecAuth> selectAuthByUserId(Long userId) { return dao.selectAuthByUserId(userId); }

    @Override
    public List<ElecMenu> selectMenuByUserId(Long userId) { return dao.selectMenuByUserId(userId); }

    @Override
    public Map<String, Object> getAuthListByRoleId(Long roleId) { return dao.getAuthListByRoleId(roleId); }

    @Override
    public ElecUser getUserInfoByMobile(String mobile) {
        ElecUser user = dao.getUserByMobile(mobile);
        UserDetails details = service.loadUserByUsername(user.getUserLoginName());
        if (Objects.isNull(details)) {
            return null;
        }
        return user;
    }

    @Override
    public boolean registerUser(ElecUser user) {
        return dao.registerUser(user) > 0;
    }

    @Override
    public int getUserByName(String userName) {
        return dao.getUserByName(userName);
    }

    @Override
    public int getMobileAysValid(String mobile) {
        return dao.getMobileAysValid(mobile);
    }

}

